package com.mo.lib.utils.res

import android.R
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import com.dylanc.longan.application
import com.mo.lib.utils.getBitmap
import com.mo.lib.utils.toDrawable

/**
 * @ author：mo
 * @ data：2019/2/13:10:18
 * @ 功能：图像选择器 的构建辅助类
 */
class DrawableSelectorBuilder {
    /** 图像选择器*/
    private val selector: StateListDrawable = StateListDrawable()

    /** 设置选择状态下的颜色             color 颜色值 */
    fun setSelectedColor(color: Int) = addState(intArrayOf(R.attr.state_selected), color)

    /** 设置可用状态下的颜色*/
    fun setEnabledColor(color: Int) = addState(intArrayOf(R.attr.state_enabled), color)

    /** 设置触按状态下的颜色*/
    fun setPressedColor(color: Int) = addState(intArrayOf(R.attr.state_pressed), color)

    /** 设置其余状态下的颜色*/
    fun setOtherStateColor(color: Int) = addState(intArrayOf(), color)

    /** 设置选择状态下的图像*/
    fun setSelectedDrawable(drawable: Drawable?) = addState(intArrayOf(R.attr.state_selected), drawable)

    /** 设置可用状态下的图像*/
    fun setEnabledDrawable(drawable: Drawable?) = addState(intArrayOf(R.attr.state_enabled), drawable)

    /**设置触按状态下的图像 */
    fun setPressedDrawable(drawable: Drawable?) = addState(intArrayOf(R.attr.state_pressed), drawable)

    /** 设置其余状态下的图像*/
    fun setOtherStateDrawable(drawable: Drawable?) = addState(intArrayOf(), drawable)

    /**添加指定状态下的颜色    stateSet 指定状态集合           color    颜色值    */
    fun addState(stateSet: IntArray?, color: Int): DrawableSelectorBuilder {
        selector.addState(stateSet, ColorDrawable(color))
        return this
    }

    /**添加指定状态下的图像    stateSet 指定状态集合             drawable Drawable 对象    */
    fun addState(stateSet: IntArray?, drawable: Drawable?): DrawableSelectorBuilder {
        selector.addState(stateSet, drawable)
        return this
    }
}
/** 资源图片 转 Drawable*/
@SuppressLint("UseCompatLoadingForDrawables")
fun  getDrawable( resId:Int): Drawable = application.resources.getDrawable(resId,null)

/**Bitmap 转 Drawable */
fun getDrawable(bitmap: Bitmap?)= toDrawable(bitmap)

/** Drawable 转 Bitmap*/
fun toBitmap(drawable: Drawable?)= getBitmap(drawable!!)